package com.stv;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TabHost;

public class MainActivity extends Activity {

    GridView mGrid;
    GridView mSecGrid;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        loadApps();
        
        setContentView(R.layout.activity_main4);
        
        prepareTabs();
        
        mGrid = (GridView) findViewById(R.id.myGrid);
        mGrid.setAdapter(new AppsAdapter());

        mSecGrid = (GridView) findViewById(R.id.mySecondGrid);
        mSecGrid.setAdapter(new SecAppsAdapter());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    private List<ResolveInfo> mApps;  

    private List<ResolveInfo> mSecApps;

    private void loadApps() {
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
        
//      mSecApps.add(mApps.get(0));
//      mSecApps.add(mApps.get(1));
//      mSecApps.add(mApps.get(2));
      
		mSecApps = getPackageManager().queryIntentActivities(mainIntent, 0);

		for (int i = mSecApps.size() - 1; i > 2; i--) {
			mSecApps.remove(i);
		}
    }
    
    private void prepareTabs() {
    	Resources res = getResources();
    	 
    	TabHost tabs=(TabHost)findViewById(android.R.id.tabhost);
    	tabs.setup();
    	 
    	TabHost.TabSpec spec=tabs.newTabSpec("mitab1");
    	spec.setContent(R.id.tab1);
    	spec.setIndicator("TAB1",
    	        res.getDrawable(android.R.drawable.ic_btn_speak_now));
    	tabs.addTab(spec);
    	 
    	spec=tabs.newTabSpec("mitab2");
    	spec.setContent(R.id.tab2);
    	spec.setIndicator("TAB2",
    	        res.getDrawable(android.R.drawable.ic_dialog_map));
    	tabs.addTab(spec);
    	 
    	tabs.setCurrentTab(0);
    }

    public class AppsAdapter extends BaseAdapter {
        public AppsAdapter() {
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i;

            if (convertView == null) {
                i = new ImageView(MainActivity.this);
                i.setScaleType(ImageView.ScaleType.FIT_CENTER);
                i.setLayoutParams(new GridView.LayoutParams(50, 50));
            } else {
                i = (ImageView) convertView;
            }

            ResolveInfo info = mApps.get(position);
            i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));

            return i;
        }


        public final int getCount() {
            return mApps.size();
        }

        public final Object getItem(int position) {
            return mApps.get(position);
        }

        public final long getItemId(int position) {
            return position;
        }
    }
    
    public class SecAppsAdapter extends BaseAdapter {
        public SecAppsAdapter() {
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i;

            if (convertView == null) {
                i = new ImageView(MainActivity.this);
                i.setScaleType(ImageView.ScaleType.FIT_CENTER);
                i.setLayoutParams(new GridView.LayoutParams(50, 50));
            } else {
                i = (ImageView) convertView;
            }

            ResolveInfo info = mSecApps.get(position);
            i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));

            return i;
        }


        public final int getCount() {
            return mSecApps.size();
        }

        public final Object getItem(int position) {
            return mSecApps.get(position);
        }

        public final long getItemId(int position) {
            return position;
        }
    }
    
}
